//
//  ZZCShopWebViewController.m
//  B段项目
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "ZZCShopWebViewController.h"

@interface ZZCShopWebViewController ()<UIWebViewDelegate>

@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) UIButton *btn;
@property (nonatomic,retain)NSString *string;
@property(nonatomic, retain)HLJRefesh *refesh;
@end

@implementation ZZCShopWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.webView.delegate = self;
    self.webView.scrollView.bounces = NO;
    self.webView.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.webView];

//    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    [hub show:YES];
//    hub.labelText = @"正在为您加载.....";
//    [self.view addSubview:hub];
    self.refesh = [[HLJRefesh alloc]init];
    self.refesh.size = CGSizeMake(50, 50);
    self.refesh.center = CGPointMake(self.view.width / 2, (self.view.height - 80) / 2);
    [self.view addSubview:self.refesh];
    [self.view bringSubviewToFront:self.refesh];
    //请求数据
    NSString *urlStr = @"http://api2.pianke.me/article/info";
    NSString *bodyStr = [NSString stringWithFormat:@"auth=&client=1&contentid=%@&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&version=3.0.6", self.contentId];

    [ZYTools postUrl:urlStr body:bodyStr result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
        
        NSMutableDictionary *dic = [result objectForKey:@"data"];

        self.string = [dic objectForKey:@"html"];
        [self.webView loadHTMLString:_string baseURL:nil];
      //  [hub hide:YES];
        [self.refesh removeFromSuperview];
    } failure:^(NSError *error) {
        [self.refesh removeFromSuperview];
      //  [hub hide:YES];
        
    }];
    
}

#pragma mark --  webView自适应
- (void)webViewDidFinishLoad:(UIWebView *)webView { //webview 自适应高度

    //字体大小
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '330%'"];

    //webview 自适应
    NSString *str1 = [NSString stringWithFormat:@"%0.2f",WIDTH-15];
    
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:
                                                     @"var script = document.createElement('script');"
                                                     "script.type = 'text/javascript';"
                                                     "script.text = \"function ResizeImages() { "
                                                     "var myimg,oldwidth;"
                                                     "var maxwidth= %@;"//缩放系数,调动这个参数可以改变图片的宽度
                                                     "for(i=0;i <document.images.length;i++){"
                                                     "myimg = document.images[i];"
                                                     "oldwidth = myimg.width;"
                                                     "if (oldwidth >= 300) {"
                                                     "myimg.width = maxwidth;"
                                                     "myimg.height = myimg.height;"
                                                     "}"
                                                     "}"
                                                     "}\";"
                                                     "document.getElementsByTagName('head')[0].appendChild(script);",str1]];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'";
    [webView stringByEvaluatingJavaScriptFromString:str];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
